﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridView的综合使用
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        public void BindGridView()
        {
            SqlConnection connection = SqlHelper.GetConnection();
            DataSet dataSet =
                SqlHelper.GetDataSetBySqlCommand(
                    @"SELECT StudentID, Student2.Name, Department.Name AS DepartmentName, Photo 
                    FROM Student2 INNER JOIN Department ON Student2.Department = Department.DepartmentID",
                    connection);

            gridStudent.DataSource = dataSet;
            Cache["DataSource"] = dataSet;
            gridStudent.DataBind();

            for (int i = 0; i < gridStudent.Rows.Count; i++)
            {
                gridStudent.Rows[i].Cells[0].Text = (i + 1).ToString();
            }
        }

        protected void gridStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridStudent.PageIndex = e.NewPageIndex;
            BindGridView();
        }
    }
}